package com.azusa.xbiao.controller;

import com.azusa.xbiao.entity.Result;
import com.azusa.xbiao.entity.StatusCode;
import com.azusa.xbiao.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    /**
     * 查询home页所需要的数据，如热销商品与最新商品
     * @return
     */
    @RequestMapping("initHome")
    public Result initHome(){
        return new Result(StatusCode.SUCCESS,goodsService.initHome());
    }

    /**
     * 查询商品页具体商品
     * @return
     */
    @RequestMapping("initGoodsDetail/{gid}/{uid}")
    public Result initGoodsDetail(@PathVariable("gid")int gid, @PathVariable("uid")int uid){
        return new Result(StatusCode.SUCCESS,goodsService.initGoodsDetail(gid, uid));
    }

    /**
     * uid用户收藏/取消收藏gid商品
     * @param gid
     * @param uid
     * @param flag
     * @return
     */
    @PutMapping("changeUserCollect/{gid}/{uid}/{flag}")
    public Result changeUserCollect(@PathVariable("gid")int gid, @PathVariable("uid")int uid,@PathVariable("flag")boolean flag){
        goodsService.changeUserCollect(gid,uid,flag);
        return new Result(StatusCode.SUCCESS);
    }

    /**
     * 查询全部商品信息
     * @param pageNum
     * @param pageSize
     * @return
     */
    @PostMapping("initProductList/{pageNum}/{pageSize}")
    public Result initProductList(@PathVariable("pageNum")Integer pageNum, @PathVariable("pageSize")Integer pageSize,@RequestBody Map<String,String> params){
        return new Result(StatusCode.SUCCESS,goodsService.initProductList(pageNum,pageSize,params.get("title")));
    }
}
